package com.home.truc.code.components;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JToolTip;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.ToolTipManager;

import com.home.Common.Common;
import com.home.Common.Resources_Truc;
import com.home.Entity.Ban;
import com.home.duy.code.DAO.BanDAO;

import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.SystemColor;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.MatteBorder;
import javax.swing.border.LineBorder;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.HashMap;
import java.util.List;

import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;

public class MyItemBan extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Point pOld = null;
	boolean press = false;
	Ban myItem = null;
	private String tenBanGhep = "";
	
	JLabel lblTinhTrang=null;
	JLabel lblTenBan = null;
	JPanel panelTinhTrang = null;
	public MyItemBan(MyJPanelKhuVucBan p, Ban value) {
		setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.window);
		
		this.myItem = value;
		///init
		//setBackground(SystemColor.control);
		setOpaque(true);
		setLayout(new BorderLayout(10, 5));
						
		lblTenBan = new JLabel();
		add(lblTenBan, BorderLayout.NORTH);
		lblTenBan.setToolTipText("Click để xem chi tiết");
		lblTenBan.setForeground(new Color(0, 51, 153));
		lblTenBan.setFont(new Font("Tahoma", Font.BOLD, 30));
	/*
		lblTenBan.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				setBackground(Color.GREEN);
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				setBackground(SystemColor.window);
			}
		});
		*/
		lblTenBan.setHorizontalAlignment(SwingConstants.CENTER);
						
		panelTinhTrang = new JPanel();
		panelTinhTrang.setBackground(SystemColor.window);
		add(panelTinhTrang, BorderLayout.SOUTH);
		panelTinhTrang.setLayout(new BorderLayout(0, 0));

		lblTinhTrang = new JLabel();
		panelTinhTrang.add(lblTinhTrang);
		lblTinhTrang.setHorizontalAlignment(SwingConstants.CENTER);
//		panelBackground.SetImage(this.myItem.getImageIcon().getImage());
		CapNhatTinhTrang();
	}
	
	//cac phuong thuc xu ly
	public void CapNhatTinhTrang()
	{
		this.lblTenBan.setText(LayTenBan());
		
		new BanDAO().CapNhatTrangThai(this.myItem);
		
		if(this.myItem.isSuDung())
		{
			if(this.myItem.isTrangThai())
			{
				this.lblTinhTrang.setText("Đang bận");
				panelTinhTrang.setBackground(Color.RED);
			}else {
				this.lblTinhTrang.setText("Bàn trống");
				panelTinhTrang.setBackground(Color.WHITE);
			}
		}else{
			this.lblTinhTrang.setText("Không được sử dụng");
			panelTinhTrang.setBackground(new Color(105, 105, 105));
		}
		this.updateUI();
	}

	public void SetTenBanGhep(String s)
	{
		this.tenBanGhep = s;
		CapNhatTinhTrang();
	}
	public String GetTenBanGhep()
	{
		return this.tenBanGhep;
	}
	public String LayTenBan()
	{
		if(this.myItem.getBanGhep()!=0)
		return this.tenBanGhep;
		else
			return this.myItem.getTen();
	}
	
	public void Select()
	{
		setBorder(new LineBorder(Color.MAGENTA, 4));
	}
	public void DeSelect()
	{
		setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	}

	public Ban getValue() {
		return myItem;
	}

	public void setValue(Ban myItem) {
		this.myItem = myItem;
	}
	
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
